/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IEntityTypeManageDao;
import com.eelpo.cmmp.uac.dao.entity.TEntityType;
import com.eelpo.cmmp.uac.dao.vo.EntityType;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 
 * 实体类型数据持久层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class EntityTypeManageDao extends BasicCommonDao implements IEntityTypeManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#getEntityTypeById(java.lang
	 *      .String)
	 */
	@Override
	public TEntityType getEntityTypeById(String id) throws DataAccessException {
		return find(TEntityType.class, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#getEntityTypeByCode(java
	 *      .lang.String)
	 */
	@Override
	public TEntityType getEntityTypeByCode(final String code) throws DataAccessException {
		List<?> list = findByNamedQuery("EntityType.findByCode", code);
		return (TEntityType) (list.isEmpty() ? null : list.get(0));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntityType.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#checkExistByUpdate(java.
	 *      lang.String, java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntityType.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#checkEntityTypeUsed(java.lang.String)
	 */
	@Override
	public boolean checkEntityTypeUsed(String id) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntityType.class, "type");
		criteria.result(Result.count());
		criteria.add(Expression.isNotEmpty("entities"));
		criteria.add(Expression.eq("id", id));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#addEntityType(com.eelpo
	 *      .cmmp.uac.dao.entity.TEntityType)
	 */
	@Override
	public String addEntityType(TEntityType entityType) throws DataAccessException {
		persist(entityType);
		return entityType.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#updateEntityType(com.eelpo
	 *      .cmmp.uac.dao.entity.TEntityType)
	 */
	@Override
	public void updateEntityType(TEntityType entityType) throws DataAccessException {
		merge(entityType);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#deleteEntityType(java.lang
	 *      .String)
	 */
	@Override
	public void deleteEntityType(String id) throws DataAccessException {
		remove(getReference(TEntityType.class, id));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#getEntityTypesPaing(com.
	 *      eelpo.cmmp.uac.dao.vo.EntityType, int, int)
	 */
	@Override
	public List<TEntityType> getEntityTypesPaing(EntityType entityType, int first, int max) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntityType.class);
		criteria.add(Expression.like("name", entityType.getName()));
		criteria.add(Expression.like("code", entityType.getCode()));
		criteria.add(Expression.eq("status", Status.valueOf(entityType.getStatus())));
		criteria.order(Order.order(entityType.getSort()));
		criteria.setFirstResult(first);
		criteria.setMaxResults(max);
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityTypeManageDao#getEntityTypesCount(com.
	 *      eelpo.cmmp.uac.dao.vo.EntityType)
	 */
	@Override
	public int getEntityTypesCount(EntityType entityType) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntityType.class);
		criteria.result(Result.count());
		criteria.add(Expression.like("name", entityType.getName()));
		criteria.add(Expression.like("code", entityType.getCode()));
		criteria.add(Expression.eq("status", Status.valueOf(entityType.getStatus())));

		return getResultCount(criteria);
	}
}
